package lib

import (
	"algorithms/ds"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         129,
		Discription: "求根节点到叶子节点的数字拼接之和",
		Level:       2,
		Labels: map[string]int{
			"二叉树": 1,
			"递归":  1,
		},
	})
}

func SumNumbers(root *ds.TreeNode) int {
	sum := 0
	var travel func(*ds.TreeNode, int)
	travel = func(node *ds.TreeNode, num int) {
		if node == nil {
			return
		}

		num = num*10 + node.Val
		if node.Left == nil && node.Right == nil {
			sum += num
			return
		}

		travel(node.Left, num)
		travel(node.Right, num)
	}

	travel(root, 0)
	return sum
}
